<?php
  /**
   * iJLanguageEditor
   *
   * The iJLanguageEditor class 
   *
   * @copyright Copyright (c) 2011, iJBloks Progect
   */
    class LanguageEditor extends iJBlock{
                
        /**
         *
         * Class Constructuctor
         * @access public
         */
        public function __construct() {
    
        }
        
        
        /**
         * execute
         *
         * Executes the block to generate the tokens
         *
         * @return array Array with the tokens of the block
         *
         * @access public
         */
        public function execute() {    
          $search = isset($_REQUEST['search']) && !empty($_REQUEST['search']) ? $_REQUEST['search'] : '';
          $page   = isset($_REQUEST['page'])   && !empty($_REQUEST['page'])   ? $_REQUEST['page']   : 1;
          $arrayToken = '';
          if(isset($_GET['blockID'])) {
            $delArray = array();
            
            $iJLanguages = iJFactory::singleton('iJLanguages');
            $block_info = iJBlock::getBlockInfo($_GET['blockID']);
            
            if(isset($_GET['trash'])) {
              $iJLanguages->trashTocken($_GET['blockID'], $_GET['trash']);
            }
                 
              if(isset($block_info['language']) && !empty($block_info['language']))  {
                 $path = '';
                 $pathTpl = '';
                 if($block_info['isAdmin'] == 1) {
                    $path    = DIR_BACKEND_LANGUAGES;
                    $pathTpl = DIR_BACKEND_TEMPLATES;
                 } else {
                    $path    = DIR_FRONTEND_LANGUAGES;
                    $pathTpl = DIR_FRONTEND_TEMPLATES;
                 }
                 $languages = $iJLanguages->getLanguages();
                 $keys = array();
                 foreach($languages as $lang) {
                   $langTokens = iJEngineXML::getArrayFromXMLFile($path . $lang['code'] . '/' . $block_info['language']);
                   $arrayToken = $arrayToken . $lang['code'] . ";";
                   if(isset($langTokens['language']['definition']['key'])) {
                     $def = $langTokens['language']['definition'];
                     unset($langTokens['language']['definition']);
                     $langTokens['language']['definition'][0] = $def;
                   }
            
                   if(empty($langTokens['language']['definition'])) {
                       continue;
                   }
                    
                   foreach($langTokens['language']['definition'] as $fileTokens)                   
                      $keys[$fileTokens['key']][$lang['code']] = $fileTokens['value'];                 
                  
                 }
                 $tpl='';
                 if(isset($block_info['template'])) {
                  $tpl = $pathTpl .  $block_info['template'];
                  $content = file_get_contents($tpl, FILE_TEXT);
  
                  foreach($keys as $key => $item){
                    if(strpos($content, '$lang.' . $key) === false){
                      $keys[$key]['used'] = 0;                 
                    } else {
                      $keys[$key]['used'] = 1;
                    }   
                  }
                  
                 }                                                
                 $this->addToken('lang_icon', iJHrefLink::Icon('language', '16x16'));
                 $this->addToken('delete_icon', iJHrefLink::Icon('trash', '16x16'));                 
                 $this->addToken('languages', $languages);
                 $this->addToken('blockID', $_GET['blockID']);
                 //Ëèñòàëêà
                 $pageArray = array();
                 $index = RESULT_LIMIT * ($page-1);                 
                 
                 $i = 0;                 
                 foreach($keys as $key => $item){
                  if($index <= $i){
                    $pageArray[$key]          = $item;
                    $pageArray[$key]['trash'] = iJHrefLink::linkToLocalPage('language_editor') .'?blockID=' . $_GET['blockID'] . '&page=' . $page . '&trash=' . $key; 
                    if(($i - $index) >= (RESULT_LIMIT - 1) ) {
                      break;
                    }                       
                  } 
                  $i++;                                                
                 }            
                        
                 if($page <= 1) {
                   $this->addToken('prev_link', iJHrefLink::linkToLocalPage('', iJHrefLink::Icon('previous_disabled', '32x32')));
                 } else {
                   $get['blockID'] = $_GET['blockID']; 
                   $get['page'] = $page - 1;
                   $this->addToken('prev_link', iJHrefLink::linkToLocalPage('language_editor', iJHrefLink::Icon('previous', '32x32'), $get));
                 }
                  
                 if($page >= ceil(count($keys) / RESULT_LIMIT)) {
                   $this->addToken('next_link', iJHrefLink::linkToLocalPage('', iJHrefLink::Icon('next_disabled', '32x32')));
                 } else {
                   $get['blockID'] = $_GET['blockID']; 
                   $get['page'] = $page + 1;                   
                   $this->addToken('next_link',  iJHrefLink::linkToLocalPage('language_editor', iJHrefLink::Icon('next', '32x32'), $get));
                 }
                  
                 $this->addToken('form_action', iJHrefLink::linkToLocalPage('language_editor')); 
                 $this->addToken('icon',        iJHrefLink::Icon('page_code', '16x16'));
                 $this->addToken('save',        iJHrefLink::Icon('save', '16x16'));
                 
                 $this->addToken('search',      $search);
                 $this->addToken('page',        $page);
                 $this->addToken('keys',        $pageArray);
                  
                 $this->addToken('total_items',    count($keys));
                 $this->addToken('count',          count($languages));

                 $this->addToken('arrayToken',     $arrayToken);
                 $this->addToken('pages_count',    ceil(count($keys) / RESULT_LIMIT));                
              }    
          }
          return parent::execute();
        }
        
                                
    }
?>